package com.bestcem.xm.qdes.handler.export.type;

import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.qdes.enums.QuestionCustomTypeEnum;
import com.bestcem.xm.qdes.enums.QuestionTypeEnum;
import com.bestcem.xm.qdes.handler.export.BaseTypeHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Component
public class ScoreHandler implements BaseTypeHandler {

    @Override
    public List<Object> getItems(JSONObject questionDict, JSONObject qgidDict, List<JSONObject> relatedRefs) {
        // 打分题
        //SingleHandler singleHandler = new SingleHandler();
        //List<Object> items = singleHandler.getItems(questionDict, qgidDict, relatedRefs);
        List<Object> items = getItems4Single(questionDict, qgidDict, relatedRefs);
        String qgid = questionDict.getString("gid");
        String qcid = questionDict.getString("cid");
        String qtitle = questionDict.getString("title");
        String qtype = questionDict.getString("qtype");
        String customQtype = questionDict.getString("custom_qtype");

        int dispSelect = (int) questionDict.getOrDefault("dispSelect", 0);  // dispSelect:0 选项设置; 1 代码设置
        boolean codeSelect = (boolean) questionDict.getOrDefault("codeSelect", true); // true: 显示代码/false: 抹灰代码
        int jumpSelect = (int) questionDict.getOrDefault("jumpSelect", 0);  // jumpSelect:0 选项设置; 1 代码设
        String dispCode = (String) questionDict.getOrDefault("disp_code", "");  // 显示代码
        String jumpCode = (String) questionDict.getOrDefault("jump_code", "");  // 跳转代码
        String autoCode = (String) questionDict.getOrDefault("auto_code", "");  // 自动勾选代码

        boolean required = questionDict.getBoolean("required");  // 必答
        List<Integer> scoreRange = castList(questionDict.getOrDefault("score_range", new ArrayList<>(Arrays.asList(0, 0))), Integer.class);
        JSONObject promptText = (JSONObject) questionDict.getOrDefault("prompt_text", new JSONObject());
        String typeStr = QuestionTypeEnum.valueOf("QUESTION_TYPE_" + qtype.toUpperCase()).getDesc();

        if (StringUtils.isNotEmpty(customQtype)) {
            try {
                QuestionCustomTypeEnum customQtypeDesc = QuestionCustomTypeEnum.valueOf("QUESTION_CUSTOM_TYPE_" + customQtype.toUpperCase());
                if (customQtypeDesc != null && StringUtils.isNotEmpty(customQtypeDesc.getDesc())) {
                    typeStr = customQtypeDesc.getDesc();
                }
            } catch (IllegalArgumentException exp) {
                log.error("No found QuestionCustomTypeEnum");
            }
        }

        String iconstr = questionDict.getString("icon");

        List<String> item0 = new ArrayList<>();
        item0.add(qcid + "[" + typeStr + "]");
        item0.add(qtitle);

        List<String> qAttr = new ArrayList<>();
        qAttr.add(required ? "必答" : "非必答");

        if (StringUtils.isNotEmpty(iconstr)) {
            qAttr.add(String.format("图标#%s", iconstr));
        }

        if ("nps".equals(customQtype)) {
            if (scoreRange.get(0) != 0) {
                qAttr.add(String.format("最低%s分", scoreRange.get(0)));
            }
            if (scoreRange.get(1) != 0) {
                qAttr.add(String.format("最低%s分", scoreRange.get(1)));
            }
        }

        if (!promptText.isEmpty()) {

            qAttr.add(String.format("提示文字#%s", (String.join(",", Arrays.asList(
                    (String) promptText.getOrDefault("left_words", ""),
                    (String) promptText.getOrDefault("middle_words", ""),
                    (String) promptText.getOrDefault("right_words", ""))))));
        }

        if (dispSelect == 1 && codeSelect) {
            qAttr.add("显示代码");
        } else if (dispSelect == 1 && !codeSelect) {
            qAttr.add("抹灰代码");
        } else {
            qAttr.add("");
        }
        // 题目属性
        List<String> allAttr = new ArrayList<>();
        qAttr.forEach(at -> {
            allAttr.add("[" + at + "]");
        });
        item0.add(String.join("", allAttr));
        // 显示代码，跳转代码，自动勾选代码
        item0.add(dispSelect == 1 ? dispCode : "");
        item0.add(jumpSelect == 1 ? jumpCode : "");
        item0.add(autoCode);
        items.set(0, item0);
        return items;
    }

    @Override
    public String getType() {
        return "score";
    }
}
